using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.Windows.Forms;
using THSLib;
namespace THSERP.BusinessEntities
{
   public class BusinessObjectFactory
    {
        public static BusinessObject GetBusinessObject(String strBusinessObjectName)
        {
            try
            {
                Type t = GetBusinessObjectType(strBusinessObjectName);
                THSUtil.countInvoke++;
                if(t !=null)
                    return (BusinessObject)THSLib.DynamicInvoker.DynamicCreateInstanceObject(t);
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static Type GetBusinessObjectType(String strBusinessObjectName)
        {
            try
            {
                Assembly aBusinessEntities = Assembly.LoadFrom(Application.StartupPath + @"\" + "THSERP.BusinessEntities.dll");
                Type tBusinessObjectType = aBusinessEntities.GetType(aBusinessEntities.GetName().Name + "." + strBusinessObjectName);
                if (tBusinessObjectType == null)
                {
                    Assembly aTHSERP = Assembly.LoadFrom(Application.StartupPath + "\\THSERP.exe");
                    tBusinessObjectType = aTHSERP.GetType(aTHSERP.GetName().Name+ "." + strBusinessObjectName);
                    if (tBusinessObjectType == null)
                    {
                        Assembly aTHSLib = Assembly.LoadFrom(Application.StartupPath + "\\THSLib.dll");
                        tBusinessObjectType = aTHSLib.GetType(aTHSLib.GetName().Name + "." + strBusinessObjectName);
                    }
                }
                return tBusinessObjectType;
            }
            catch (Exception)
            { return null; }
        }
    }
}
